package com.dems.web;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.dems.dao.AdministratorDao;
import com.dems.dao.MpDao;
import com.dems.dao.StudentDao;
import com.dems.domain.Administrator;
import com.dems.domain.Mp;
import com.dems.domain.Student;

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private StudentDao studentDao = new StudentDao();
	private AdministratorDao administratorDao = new AdministratorDao();
	private MpDao mpDao = new MpDao();
    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String accountId = request.getParameter("accountId");
		String password = request.getParameter("password");
		String identity = request.getParameter("identity");
		if (identity.equals("student")) {
			Student student = studentDao.login(accountId,password);
			if(student !=null) {
				//将student存入session中
				//request.setAttribute("student", student);
	            request.getSession().setAttribute("student",student);
	            request.getRequestDispatcher("/function_student.jsp").forward(request,response);
			}else {
				request.setAttribute("msg","用户名或者密码错误");
	            request.getRequestDispatcher("/login.jsp").forward(request,response);
			}
		}else if(identity.equals("administrator")) {
			Administrator administrator = administratorDao.login(accountId, password);
			if(administrator !=null) {
				//将administrator存入session中
	            request.getSession().setAttribute("administrator",administrator);
	            response.sendRedirect(request.getContextPath()+"/function_admin.jsp");
			}else {
				request.setAttribute("msg","用户名或者密码错误");
	            request.getRequestDispatcher("/login.jsp").forward(request,response);
			}
		}else {
			Mp mp = mpDao.login(accountId,password);
			if(mp !=null) {
				//将mp存入session中
				request.getSession().setAttribute("mp",mp);
				response.sendRedirect(request.getContextPath()+"/function_mp.jsp");
			}else {
				request.setAttribute("msg","用户名或者密码错误");
				request.getRequestDispatcher("/login.jsp").forward(request,response);
			}
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
